//
//  ManHuaClassifyViewController.m
//  Double
//
//  Created by 孟令博 on 15/5/22.
//  Copyright (c) 2015年 袁庆秋. All rights reserved.
//

#import "ManHuaClassifyViewController.h"
#import "ManHuaClassifyViewCell.h"
#import <AFNetworking/AFHTTPRequestOperationManager.h>
#import <UIImageView+WebCache.h>
#import "ManHuaClassifyModel.h"
#import "CollectionReusableView.h"
#import "ManHuaDetailsViewController.h"

@interface ManHuaClassifyViewController ()<UICollectionViewDataSource,UICollectionViewDelegate>
@property (nonatomic, retain) NSMutableDictionary *dic1;
//解析完成最外层数组
@property (nonatomic, retain) NSArray *array;
@property (nonatomic, retain) NSMutableArray *smallArray1;

@end

@implementation ManHuaClassifyViewController

- (void)dealloc
{
    [_smallArray1 release];
    [_array release];
    [_dic1 release];
    [_collection release];
    [super dealloc];
}
- (instancetype)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        self.dic1 = [NSMutableDictionary dictionary];
        self.array = [NSArray array];
        self.smallArray1 = [NSMutableArray array];
    }
    return self;
}
#pragma mark -
#pragma mark 网络请求
- (void)startConnection
{
    AFHTTPRequestOperationManager *m = [AFHTTPRequestOperationManager manager];
    m.responseSerializer = [AFHTTPResponseSerializer serializer];
    NSString *str = [NSString stringWithFormat:@"http://manhua.haomee.cn/?m=Cartoon&ios=23&pad=0&a=channelgroup"];
    m.responseSerializer = [AFHTTPResponseSerializer serializer];
    [m GET:str parameters:nil success:^(AFHTTPRequestOperation *operation, id responseObject) {
        NSData *data = operation.responseData;
        self.array = [NSJSONSerialization JSONObjectWithData:data options:   NSJSONReadingMutableContainers error:nil];

        for (NSDictionary *dic in self.array) {
            self.dic1 = [NSMutableDictionary dictionary];
            [_dic1 addEntriesFromDictionary:dic];
            NSString *str = [self.dic1 objectForKey:@"group"];
            [self.smallArray1 addObject:str];
           
}
        [self.collection reloadData];
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        
    }];
    
}


#pragma mark -
#pragma mark 定义collectionView
- (void)myCollectionView
{
//定义layout
    UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc] init];
    layout.itemSize = CGSizeMake([UIScreen mainScreen].bounds.size.width / 3 - 20, 130);
    layout.sectionInset = UIEdgeInsetsMake(10, 20, 10, 20);
    layout.headerReferenceSize = CGSizeMake(10, 30);
    
    self.collection = [[UICollectionView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height - 110) collectionViewLayout:layout];
    _collection.dataSource = self;
    _collection.delegate = self;
    [self.collection setBackgroundColor:[UIColor whiteColor]];
    [self.collection registerClass:[ManHuaClassifyViewCell class] forCellWithReuseIdentifier:@"cell"];
    //注册section标题和结尾的重用池
    [self.collection registerClass:[CollectionReusableView class] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"header"];
    [self.view addSubview:_collection];
    [layout release];
    [_collection release];
}
#pragma mark -
#pragma mark collection协议
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    NSMutableDictionary *dic = [self.array objectAtIndex:section];
    NSMutableArray *array = [dic objectForKey:@"list"];
    return [array count];
}
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    ManHuaClassifyViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"cell" forIndexPath:indexPath];
    
//存到数组中
    NSMutableDictionary *dic1 = [self.array objectAtIndex:indexPath.section];
    NSMutableArray *array = [dic1 objectForKey:@"list"];
    NSMutableArray *bigArray = [NSMutableArray array];
    for (NSDictionary *dic in array) {
        ManHuaClassifyModel *model = [[ManHuaClassifyModel alloc] init];
        [model setValuesForKeysWithDictionary:dic];
        [bigArray addObject:model];
        
        [model release];
    }
    ManHuaClassifyModel *model = [bigArray objectAtIndex:indexPath.item];
    cell.priceLable.text = model.name;
    [cell.myImage sd_setImageWithURL:[NSURL URLWithString:model.pic] placeholderImage:[UIImage imageNamed:@"placeholder.jpg"] options:SDWebImageRetryFailed];
    return cell;
}

#pragma mark -
#pragma mark section标题

-(NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView
{
    return self.array.count;
}
- (UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath
{
        CollectionReusableView *header = [collectionView dequeueReusableSupplementaryViewOfKind:kind withReuseIdentifier:@"header" forIndexPath:indexPath];
    header.alabel.text = [self.smallArray1 objectAtIndex:indexPath.section];
    return header;
}
#pragma mark -
#pragma mark cell点击事件
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
    ManHuaDetailsViewController *details = [[ManHuaDetailsViewController alloc] init];
    NSMutableDictionary *dic1 = [self.array objectAtIndex:indexPath.section];
    NSMutableArray *array = [dic1 objectForKey:@"list"];
    NSMutableArray *bigArray = [NSMutableArray array];
    for (NSDictionary *dic in array) {
        ManHuaClassifyModel *model = [[ManHuaClassifyModel alloc] init];
        [model setValuesForKeysWithDictionary:dic];
        [bigArray addObject:model];
        [model release];
    }
    ManHuaClassifyModel *model = [bigArray objectAtIndex:indexPath.item];
    details.ID = model.ManHuaId;
    details.navigationItem.title = model.name;
    details.hidesBottomBarWhenPushed = YES;
    [self.navigationController pushViewController:details animated:YES];
    [details release];
}
- (void)viewDidLoad {
    [super viewDidLoad];
//    self.navigationController.navigationBar.translucent = NO;
    [self myCollectionView];
    [self startConnection];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}
@end
